package com.itjavas.cslp2.controller;

//import com.itjavas.cslp2.pojo.entity.User;
//import com.itjavas.cslp2.mapper.UserMapper;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.*;
//
//@RestController
//@RequestMapping("/user")
//
//public class UserController {
//
//    @Autowired
//    UserMapper userMapper;
//    @RequestMapping("/signIn")
//    public boolean signIt(Integer userId){
//        return userMapper.point(userId);
//    }
//    @CrossOrigin
//    //用于解决跨域报错的注解
//    @RequestMapping("/login")
//    public Boolean loginIn(User user1){
//        System.out.println(user1.getName());
//        System.out.println("hello");
//        User user=userMapper.login(user1.getName());
//        if (user!=null&&user.getPassword().equals(user1.getPassword())){
//            return true;
//        }
//        else {
//            return false;
//        }
//    }
//    @RequestMapping("/register")
//    public boolean register(String userName,String userPassword){
//        return userMapper.register(userName,userPassword);
//    }
//}
//@RequestMapping("/register")
//@CrossOrigin
//public boolean register(User user) {
//    System.out.println(user.getName());
//    System.out.println(user.getPassword());
//    return userMapper.register(user.getName(), user.getPassword());
//    }
//}
import com.itjavas.cslp2.pojo.entity.Course;
import com.itjavas.cslp2.pojo.entity.User;
import com.itjavas.cslp2.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;
    //解决报错注解
    @ResponseBody
    @CrossOrigin

    //登录登录登录登录
    @PostMapping("/login")
    public ResponseEntity<Boolean> loginIn(@RequestBody User user) throws NoSuchAlgorithmException {
        System.out.println(user.getPassword());
        user.setPassword(getSHA(user.getPassword()));//密码加密
        System.out.println(user.getPassword());
        return userService.login(user);
    }

    //注册
    @CrossOrigin
    @RequestMapping("/register")
    public ResponseEntity<Boolean> register(@RequestBody User user) throws NoSuchAlgorithmException {
        System.out.println(user.getPassword());
        user.setPassword(getSHA(user.getPassword()));//密码加密
        System.out.println(user.getPassword());
        return userService.register(user);
    }

    //主页面的用户信息
    @CrossOrigin
    @RequestMapping("/getUserInfo")
    public ResponseEntity<User> getUserInfo(@RequestParam String name){
        //没有查密码
        return ResponseEntity.ok(userService.getUserInfo(name));
    }

    //编辑用户信息
    @RequestMapping("/editUser")
    public ResponseEntity<Boolean> editUser(@RequestBody User user) throws NoSuchAlgorithmException {
        user.setPassword(getSHA(user.getPassword()));//密码加密
        return ResponseEntity.ok(userService.editUser(user));
    }
//编辑账户使用状态
    @RequestMapping("/editStatus")
    public ResponseEntity<Boolean> editStatus(@RequestBody User user) throws NoSuchAlgorithmException {
        user.setPassword(getSHA(user.getPassword()));//密码加密
       return ResponseEntity.ok(userService.editStatus(user));
    }
//修改用户密码
    @RequestMapping("/editPassword")
    public ResponseEntity<Boolean> editPassword(@RequestBody User user) throws NoSuchAlgorithmException {
        user.setPassword(getSHA(user.getPassword()));
        return ResponseEntity.ok(userService.editPassword(user));
    }
    //根据用户获取选课信息
    @RequestMapping("/getRecordByUser")
    public ResponseEntity<List<Course>> getRecordByUser(@RequestBody User user)
    {
        return userService.getRecordByUser(user);
    }
    //哈希密码加密
    public static String getSHA(String str) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        byte[] shaBytes = md.digest(str.getBytes());
        return bytesToHexString(shaBytes);
    }

    //将shaBytes转化成String(16进制）
    public static String bytesToHexString(byte[] bytes) {
        StringBuilder stringBuilder = new StringBuilder();
        if (bytes == null || bytes.length <= 0) {
            return null;
        }
        for (byte aByte : bytes) {
            int v = aByte & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }


}
